/**
 * 字典
 */
export default class Dictionary{
    private _dic:{[key:string]:any} = {}
    private __length:number = 0;
    public del(key:string):any{
        let item = null;
        if(this.has(key)){
            item = this._dic[key];
            delete this._dic[key];
            this.__length--;
        }
        return item;
    }
    public isEmpty(){
        return !!this.__length;
    }
    public select(key:string):any{
        if(this.has(key)){
            return this._dic[key];
        }
        return null;
    }
    public get(key:string):any{
        return this._dic[key];
    }
    public insert(key:string,item:any):boolean{
        if(typeof key !== "string")
            return false;
        if(this.has(key)){
            return false;
        }
        this._dic[key] = item;
        this.__length++;
        return true
    }
    public has(key:string):boolean{
        if(this._dic[key] !== undefined){
            return true;
        }
        return false
    }
    public size():number{
        return this.__length;
    }
    public clear():void{
        this._dic = {};
        this.__length = 0;
    }
    public toString():string{
        let keys = Object.keys(this._dic)
        let str = ""
        for(let i = 0;i<keys.length;i++){
            str += `[${keys[i]},${this._dic[keys[i]]}]`
        }
        return str;
    }
}